<?php

class My_Content {

    function  __construct($graphName = null, $wertexId = null) {
        Zend_Loader::loadClass('My_Graph');
        Zend_Loader::loadClass('My_Link');
        Zend_Loader::loadClass('My_Wertex');
        Zend_Loader::loadClass('My_Object');

        $this->graphName = $graphName;
        $this->wertexId = $wertexId;

        if(!empty($this->graphName)){
            $this->loadByName();
        }

        if(!empty($this->wertexId)){
            $this->loadByWertexId();
        }

    }

    function loadByName(){
        if(empty($this->graphName)){
            return false;
        }

        $graph = new My_Graph();
        $graph->readByName($this->graphName);

        $link = new My_Link();
        $this->links = $link->readByStart($graph->getFirst());
        $this->loadObjects($this->links);
    }


    // nolasa beigu virsotnes tekošajai
    function loadByWertexId(){
        if(empty($this->wertexId)){
            return false;
        }
        
        $link = new My_Link();
        $this->links = $link->readByStart($this->wertexId);
        $this->loadObjects($this->links);
    }
    
    function loadObjects($links){
        foreach($links AS $i => $data){
            $link = new My_Link($data);
            $wertex = new My_Wertex($link->getEnd());
            $object = new My_Object($wertex->getObject());

            foreach($object->getPluginClassNames() AS $j => $plugin){
                Zend_Loader::loadClass('Plugin_'.$plugin.'_Class');
                // object vaig pārsaukt par plugin
                eval('$this->objects[$i][$j] = new Plugin_'.$plugin.'_Class($wertex->getId());');
            }
        }
    }

    function getContent($link = false, $options = false, $show = null){
        if(!is_array($this->objects)){
            return false;
        }

        $user['type'] = 'guest'; // default
        $auth = Zend_Auth::getInstance();
        if(Zend_Auth::getInstance()->hasIdentity()){
            $user = $auth->getIdentity();
        }
        

        foreach($this->objects AS $i => $object){
            foreach($object AS $j => $plugin){

                if(method_exists($this->objects[$i][$j], "userAccessAllowed")){
                    $data[$i]['user_access_allowed'] = $this->objects[$i][$j]->userAccessAllowed($user['type']);
                }else{
                    if($plugin->isVisual()){

                        $print = true;
                        if(is_array($show)){
                            if(!in_array($this->objects[$i][$j]->getName(), $show)){
                                $print = false;
                            }
                        }

                        if($print){
                            if($link){
                                $data[$i]['content'] = sprintf(
                                        '<a href="%s">%s</a>',
                                        $this->objects[$i][$j]->getUrl().'public/module/show/id/'.
                                        $this->objects[$i][$j]->getWertex(),
                                        $this->objects[$i][$j]->getContent()
                                    );
                            }else{
                                $data[$i]['content'] = $this->objects[$i][$j]->getContent();
                            }
                        
                            if(is_array($options)){
                                foreach($options AS $template){
                                    $data[$i]['options'][] = sprintf($template, $this->objects[$i][$j]->getWertex());
                                }
                            }
                        }
                    }
                }
            }
        }
        return $this->getAllowedContent($data);
    }

    function getAllowedContent($data){
        foreach($data AS $key => $value){
            $cnt = $value['content'];

            if(array_key_exists('user_access_allowed', $value)){
                if(!$value['user_access_allowed']){
                    $cnt = null;
                }
            }

            if(is_array($value['options']) AND !empty($cnt)){
                $cnt .= ' - '.implode('', $value['options']);
            }
            

            if(!empty($cnt)){
                $content[] = $cnt;
            }
        }
        return $content;
    }

    function setUserType($userType){
        $this->userType = $userType;
    }

    function getContentBetweenTags($tagFirst, $tagLast, $link = false, $options = false, $show = null){
        if(!empty($show) AND !is_array($show)){
            $show = array($show);
        }

        $content = $this->getContent($link, $options, $show);
        if(!is_array($content)){
            return false;
        }
        foreach($content AS $item){
            $out .= $tagFirst.$item.$tagLast;
        }
        return $out;
    }
}
?>
